package mianxiangduixiang.Test8;

/**
 * @Author JetZhang
 * Package:mianxiangduixiang.Test8
 * Description:编写一个算术测试小软件，用来训练小学生的算术能力。其中两个操作数及运算符都是在点击获取题目时随机生成
 * @Date 2021/11/18
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class Teacher {
    int numberOne, numberTwo;
    String operator = "";
    boolean right;

    public int giveNumberOne(int n) {
        numberOne = (int) (Math.random() * n) + 1;
        return numberOne;
    }

    public int giveNumberTwo(int n) {
        numberTwo = (int) (Math.random() * n) + 1;
        return numberTwo;
    }

    public String giveOperator() {
        double d = Math.random();
        if (d >= 0.8)
            operator = "+";
        else if (d < 0.8 && d > 0.5)
            operator = "-";
        else if (d <= 0.5 && d > 0.2)
            operator = "*";
        else if (d <= 0.2)
            operator = "/";
        return operator;
    }

    public boolean getRight(int answer) {
        if (operator.equals("+")) {
            right = answer == numberOne + numberTwo;
        } else if (operator.equals("-")) {
            right = answer == numberOne - numberTwo;
        } else if (operator.equals("*")) {
            right = answer == numberOne * numberTwo;
        } else if (operator.equals("/")) {
            right = answer == numberOne / numberTwo;
        }
        return right;
    }
}

public class T2 extends JFrame implements ActionListener {
    JTextField textOne, textTwo, textResult;
    JButton getProblem, giveAnswer;
    JLabel operatorLabel, message;
    Teacher teacher;

    T2(String s) {
        super(s);
        teacher = new Teacher();
        setLayout(new FlowLayout());
        textOne = new JTextField(10);     //创建textOne,其可见字符长是10
        textTwo = new JTextField(10);     //创建textTwo,其可见字符长是10
        textResult = new JTextField(10);  //创建textResult,其可见字符长是10
        operatorLabel = new JLabel("+");  //创建 operatorLabel，初始值“+”
        message = new JLabel("你还没有回答呢");   //创建message，初始值“你还没有回答呢”
        getProblem = new JButton("获取题目"); //创建getProblem，初始值“获取题目”
        giveAnswer = new JButton("确认答案"); //创建giveAnswer，初始值“确认答案”
        add(getProblem);
        add(textOne);
        add(operatorLabel);
        add(textTwo);
        add(new Label("="));
        add(textResult);
        add(giveAnswer);
        add(message);//添加若干组件到窗口
        textResult.requestFocus();//textResult 获得焦点
        textOne.setEditable(false);
        textTwo.setEditable(false);//textOne设置textOne和textTwo不可编辑
        getProblem.addActionListener(this);//将当前窗口注册为getProblem的ActionEvent事件监视器
        giveAnswer.addActionListener(this);//将当前窗口注册为giveAnwser的ActionEvent事件监视器
        textResult.addActionListener(this);//将当前窗口注册为textResult的ActionEvent事件监视器
        setBounds(100, 100, 950, 100);//设置出现屏幕上时的初始位置，x=100,y=100,width=450,height=100
        setLocationRelativeTo(null);//设置窗体居中显示
        setVisible(true);//窗口的可见性设置
        validate();
        addWindowFocusListener(new WindowAdapter() {
                                   @Override
                                   public void windowClosing(WindowEvent e) {
                                       System.exit(0);
                                   }
                               }
        );
    }

    public static void main(String[] args) {
        T2 t2 = new T2("算术测试");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == getProblem)//如果点击了“获取题目 ”
        {
            int number1 = teacher.giveNumberOne(100);//获取第一个操作数的值
            int number2 = teacher.giveNumberTwo(100);//获取第二个操作数的值
            String operator = teacher.giveOperator();//获取运算符
            textOne.setText("" + number1);
            textTwo.setText("" + number2);
            textResult.setText(null);
            operatorLabel.setText(operator);
            message.setText("请回答");//设置图形界面上文本框以及标签的文本，包括：textOne,textTwo,textResult，operatorLabel,message
        }
        if (e.getSource() == giveAnswer)//如果点击了“确认答案 ”
        {
            String answer = textResult.getText();//获取文本框中输入的答案
            try {
                int result = Integer.parseInt(answer);
                if (teacher.getRight(result)) {
                    message.setText("你回答正确");//如果答案正确，message显示“你回答正确”，如果不正确，显示“你回答错误”
                } else {
                    message.setText("你回答错误");
                }
            } catch (NumberFormatException ex) {
                message.setText("请输入数字字符");//如果输入的不是数字，显示“请输入数字字符”
            }
        }
        textResult.requestFocus();//textResult 获得焦点
        validate();
    }
}

